home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / msgq160s.arc / SCREEN.C < prev    next >
Text File  |  1991-10-26  |  14KB  |  688 lines

  1. /*
  2.  * SCREEN.C - Display functions
  3.  *
  4.  * Msged/Q message editor for QuickBBS  Copyright 1990 by P.J. Muller
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdarg.h>
  12. #include <dos.h>
  13. #include <ctype.h>
  14.  
  15. #include "msged.h"
  16. #include "screen.h"
  17.  
  18. #if !defined(MK_FP)
  19. #define MK_FP(seg,ofs)      ((void far *) (((unsigned long)(seg) << 16) | \
  20.                             (unsigned)(ofs)))
  21. #endif
  22.  
  23. #if USEFOSSIL
  24. #include "vfossil.h"
  25.  
  26. static void vfossil_init (void);
  27. static void vfossil_open (void);
  28. static void vfossil_close (void);
  29. static void vfossil_cursor (int st);
  30.  
  31. static int (pascal *vfossil_funcs[20]) () = {
  32.    NULL, NULL, NULL,
  33.    NULL, NULL, NULL,
  34.    NULL, NULL, NULL,
  35.    NULL, NULL, NULL,
  36.    NULL, NULL, NULL,
  37.    NULL, NULL, NULL,
  38.    NULL, NULL
  39. };
  40. #endif
  41.  
  42. #define FALSE 0
  43. #define TRUE !FALSE
  44. #define TEXTLEN 200
  45.  
  46. int int86(int intnum,union REGS *i, union REGS *o);
  47. int int86x(int intnum,union REGS *i, union REGS *o,struct SREGS *s);
  48.  
  49. unsigned int *macros[40];    /* function key macros */
  50. int maxx;        /* maximum screen columns */
  51. int maxy;        /* maximum screen rows */
  52. int videomethod;        /* (extern in msged.h) how to write to the screen */
  53. unsigned int vbase;     /* where screen memory is located */
  54.  
  55. static unsigned int far *screen = NULL;
  56. static unsigned int cx = 1;
  57. static unsigned int cy = 1;
  58. static int desqview = 0;
  59.  
  60. #if USEFOSSIL
  61.  
  62. static struct FOSSIL_DATA {
  63.         int     length;
  64.         char    mode;
  65.         char    maxcolors;
  66.         int     maxx;
  67.         int     maxy;
  68.         int     fill1;
  69.         int     fill2;
  70.         long    fill3;
  71. } fossil_data;
  72.  
  73. static unsigned char scroll_fill [2];
  74. #endif
  75.  
  76. static unsigned char current_color;
  77.  
  78. void    video_init()
  79. {
  80.         union REGS r;
  81.         struct SREGS sr;
  82.         int vmode = 0;
  83.  
  84.         if (videomethod == FOSSIL) {
  85. #if !USEFOSSIL
  86.                 fputs("Fossil video not supported by this version\n",stderr);
  87.         halt(255);
  88. #else
  89.                 vfossil_init ();                /* Start Video FOSSIL */
  90. /**/        fossil_data.length = /* 2 * */ sizeof (fossil_data);
  91.         VioGetMode (&fossil_data,0);
  92.                         /* Get mode info      */
  93.                 maxx = fossil_data.maxx;        /* Maximum 'X' value  */
  94.                 maxy = fossil_data.maxy;        /* Maximum 'Y' value  */
  95. #endif
  96.         }
  97.         else {
  98.                 r.h.ah = 0x0f;
  99.                 int86(0x10,&r,&r);
  100.                 vmode = r.h.al;
  101.                 if (maxx == 0)
  102.                         maxx = (int) r.h.ah;
  103.  
  104.                 if (maxy == 0) {
  105.                         r.x.ax = 0x1130;
  106.                         r.x.dx = maxy;
  107.                         int86(0x10,&r,&r);
  108.                         maxy = (r.x.dx == 0) ? 25 : (r.x.dx + 1);
  109.                 }
  110.  
  111.         if ((videomethod == DIRECT) || (videomethod == NOSNOW)) {
  112.                         if (vbase == 0)
  113.                                 if (vmode == 0x07)
  114.                                         vbase = 0xb000;
  115.                                 else
  116.                                         vbase = 0xb800;
  117.  
  118.                         r.h.ah = 0xfe;
  119.                         sr.es = vbase;
  120.                         r.x.di = 0;
  121.                         int86x(0x10,&r,&r,&sr);
  122.                         desqview = (vbase != sr.es);
  123.                         vbase = sr.es;
  124.  
  125.                         screen = (unsigned int far *) MK_FP(vbase,r.x.di);
  126.                 }
  127.         }
  128. }
  129.  
  130. void    video_end()
  131. {
  132. #if USEFOSSIL
  133.         if (videomethod == FOSSIL) {
  134.                 vfossil_close ();
  135.         }
  136. #endif
  137. }
  138.  
  139. void    video_update()
  140. {
  141. #if USEFOSSIL
  142.         if (videomethod == FOSSIL) {
  143.                 VioSetCurPos (cy - 1, cx - 1, 0);
  144.         } else
  145. #endif
  146.     {
  147.                 union REGS r;
  148.  
  149.             r.h.ah = 2;
  150.             r.h.bh = 0;
  151.                 r.h.dh = (char) cy - 1;
  152.                 r.h.dl = (char) cx - 1;
  153.                 int86(0x10, &r, &r);
  154.         }
  155. }
  156.  
  157. void    scrollup(x1, y1, x2, y2, lines)
  158.     int     x1, y1, x2, y2, lines;
  159.  
  160. {
  161.         y2 = min(y2,maxy) - 1;
  162.         y1 = min(y1,maxy) - 1;
  163.         x1 = min(x1,maxx) - 1;
  164.         x2 = min(x2,maxx) - 1;
  165.  
  166. #if USEFOSSIL
  167.         if (videomethod == FOSSIL) {
  168.                 scroll_fill [0] = ' ';
  169.         scroll_fill [1] = current_color;
  170.                 if (lines == 0)
  171.                         lines = -1;
  172.  
  173.         VioScrollUp (y1,x1,y2,x2,lines,scroll_fill,0);
  174.         } else
  175. #endif
  176.         {
  177.                 union REGS      r;
  178.  
  179.             r.h.ah = 6;
  180.             r.h.al = (char) lines;
  181.                 r.h.ch = (char) y1;
  182.                 r.h.cl = (char) x1;
  183.                 r.h.dh = (char) y2;
  184.                 r.h.dl = (char) x2;
  185.         r.h.bh = current_color;
  186.                 int86(0x10, &r, &r);
  187.         }
  188. }
  189.  
  190. void    scrolldown(x1, y1, x2, y2, lines)
  191.     int     x1, y1, x2, y2, lines;
  192.  
  193. {
  194.         y2 = min(y2,maxy) - 1;
  195.         y1 = min(y1,maxy) - 1;
  196.         x1 = min(x1,maxx) - 1;
  197.         x2 = min(x2,maxx) - 1;
  198.  
  199. #if USEFOSSIL
  200.         if (videomethod == FOSSIL) {
  201.                 scroll_fill [0] = ' ';
  202.         scroll_fill [1] = current_color;
  203.  
  204.         VioScrollDn (y1,x1,y2,x2,lines,scroll_fill,0);
  205.         } else
  206. #endif
  207.         {
  208.             union REGS      r;
  209.  
  210.             r.h.ah = 7;
  211.             r.h.al = (char) lines;
  212.                 r.h.ch = (char) y1;
  213.                 r.h.cl = (char) x1;
  214.                 r.h.dh = (char) y2;
  215.                 r.h.dl = (char) x2;
  216.         r.h.bh = current_color;
  217.                 int86(0x10, &r, &r);
  218.         }
  219. }
  220.  
  221. extern void wrscrch(unsigned int far *addr, unsigned int chattr);
  222.  
  223. void bputc(unsigned char c)
  224. {
  225.   union REGS r;
  226.   unsigned int d = 0;
  227.  
  228.   d = (c & 0xff) | (current_color << 8);
  229.  
  230.   switch (videomethod) {
  231.     case DIRECT:
  232.       *(screen + ((((cy - 1) * maxx) + (cx - 1)))) = d;
  233.       break;
  234.     case NOSNOW:
  235.       wrscrch(screen + ((((cy - 1) * maxx) + (cx - 1))), d);
  236.       break;
  237. #if USEFOSSIL
  238.     case FOSSIL: {
  239.       unsigned int far *cell_ptr = &d;
  240.         VioWrtCellStr (cell_ptr, 2, cy - 1, cx - 1, 0);
  241.       break;
  242.     }
  243. #endif
  244.     default:
  245.       video_update();
  246.       r.h.ah = 0x9;
  247.       r.h.bh = 0;
  248.       r.h.al = c;
  249.       r.h.bl = current_color;
  250.       r.x.cx = 1;
  251.       int86(0x10,&r,&r);
  252.       break;
  253.   } /* switch */
  254.  
  255.   if (++cx > maxx) {
  256.     cx = 1;
  257.     if (++cy > maxy)
  258.       cy = 1;
  259.   } /* if */
  260. } /* bputc */
  261.  
  262. void    gotoxy(int x, int y)
  263. {
  264.         cx = min(x,maxx);
  265.         cy = min(y,maxy);
  266. }
  267.  
  268. int     wherex()
  269. {
  270.         return (cx);
  271. }
  272.  
  273. int     wherey()
  274. {
  275.         return(cy);
  276. }
  277.  
  278. int     getkey()
  279. {
  280.         union REGS r;
  281.     static unsigned int *macro;
  282.  
  283.         if (macro) {
  284.             if (*(++macro))
  285.                 return(*macro);
  286.  
  287.             macro = NULL;
  288.         }
  289.  
  290.  
  291. #if USEFOSSIL
  292.         if (videomethod == FOSSIL) {
  293.                 r.h.ah = 0x0e;
  294.                 int86 (0x14,&r,&r);
  295.         }
  296.         else
  297. #endif
  298.         {
  299.                 r.h.ah = 0;
  300.                 int86(0x16,&r,&r);
  301.         }
  302.     if (r.h.al) {            /* Must use FOSSIL keymap,   */
  303.                 r.h.ah = 0;             /* No scan code if have char */
  304.                 return(r.x.ax);
  305.         }
  306.  
  307.         if ((r.h.ah >= 0x3b) && (r.h.ah <= 0x44))
  308.             macro = macros[r.h.ah - 0x3b];
  309.         else if ((r.h.ah >= 0x54) && (r.h.ah <= 0x71))
  310.             macro = macros[r.h.ah - 0x4a];
  311.  
  312.         if (macro) {
  313.             if (*macro)
  314.                 return(*macro);
  315.  
  316.             macro = NULL;
  317.         }
  318.  
  319.         return(r.x.ax);
  320. } /* getkey */
  321.  
  322. int keypressed()
  323. {
  324.         union REGS r;
  325.  
  326. #if USEFOSSIL
  327.         if (videomethod == FOSSIL) {
  328.         r.h.ah = 0x0d;
  329.         int86 (0x14,&r,&r);
  330.         return (r.x.ax != 0xffff);
  331.         }
  332. #endif
  333.     r.h.ah = 1;
  334.     int86(0x16,&r,&r);
  335.     return !(r.x.flags & 0x40);
  336.  
  337. } /* keypressed */
  338.  
  339. extern void wrscrstr(unsigned int far *addr, char far *str, int len, BYTE attr);
  340.  
  341. void bputs(char *s)
  342. {
  343.   if (s == NULL)
  344.     return;
  345.  
  346.   switch (videomethod) {
  347.     default:
  348.     case DIRECT:
  349.     case BIOS:
  350.       while ((*s) && (wherex() < maxx))
  351.     if (*s != '\n')
  352.       bputc(*s++);
  353.     else
  354.       break;
  355.       break;
  356.  
  357. #if USEFOSSIL
  358.     case FOSSIL: {
  359.       char *e;
  360.       int  l;
  361.       int a = current_color;
  362.  
  363.       if ((e = strchr(s,'\n')) == NULL)
  364.     e = strchr(s,'\0');
  365.       if ((l = e - s) < 1)
  366.     return;
  367.       VioWrtCharStrAtt(s,l,cy - 1,cx - 1,&a,0);
  368.       cx += l;
  369.       break;
  370.     }
  371. #endif
  372.  
  373.     case NOSNOW: {
  374.       int l;
  375.       char *e;
  376.  
  377.       if ((e = strchr(s,'\n')) != NULL)
  378.     l = e - s;
  379.       else
  380.     l = strlen(s);
  381.       if (l < 1)
  382.     break;
  383.  
  384.       wrscrstr(screen + ((((cy - 1) * maxx) + (cx - 1))), (char far *) s,
  385.         l, current_color);
  386.       cx += l;
  387.       break;
  388.     }
  389.   } /* switch */
  390. } /* bputs */
  391.  
  392. void    cls()
  393. {
  394.         scrollup(1, 1, maxx, maxy, 0);
  395.     gotoxy(1, 1);
  396. }
  397.  
  398. void    clrwnd(x1, y1, x2, y2)
  399.     int     x1, y1, x2, y2;
  400.  
  401. {
  402.         scrollup(x1, y1, x2, y2, 0);
  403.         gotoxy(x1,y1);
  404. }
  405.  
  406. void    clreol()
  407. {
  408.         clrwnd(cx,cy,maxx,cy);
  409. }
  410.  
  411. int     bgets(char *s1, int c)
  412. {
  413.     int     ch;
  414.     int     x1;
  415.     char   *t;
  416.     static  BOOLEAN insert = ON;
  417.     static    char s[TEXTLEN];
  418.  
  419.     int    y = wherey();
  420.     int     x = wherex();
  421.  
  422.     strncpy(s, s1, sizeof s);
  423.     t = s + strlen(s);
  424.     *t = '\0';
  425.     bputs(s);
  426.  
  427.     while (TRUE) {        /* endless loop */
  428.         video_update();
  429.         switch (ch = getkey()) {
  430.  
  431.         case UP:
  432.         case DOWN:
  433.         case PGUP:
  434.         case PGDN:
  435.         case ENTER:
  436.             strcpy(s1, s);
  437.             return (ch);
  438.  
  439.         case WORDRT:
  440.             while ((*t != EOS) && (!isspace(*t)))
  441.                 ++t;
  442.             while ((*t != EOS) && (isspace(*t)))
  443.                 ++t;
  444.             gotoxy(x + (t-s), y);
  445.             break;
  446.  
  447.         case WORDLT:
  448.             if (t == s)
  449.                 break;
  450.  
  451.             while ((t > s) && (isspace(*(t-1))))
  452.                 --t;
  453.             while ((t > s) && (!isspace(*(t-1))))
  454.                 --t;
  455.  
  456.             gotoxy(x + (t-s), y);
  457.             break;
  458.  
  459.         case ABORT:        /* Esc */
  460.             return ABORT;
  461.  
  462.         case DELMSG:        /* Alt-D */
  463.         case 'Y'-'@':        /* Ctrl-Y */
  464.             memset(s, 0, sizeof s);
  465.             t = s;
  466.             gotoxy(x, y);
  467.             clreol();
  468.             break;
  469.  
  470.         case GOBOL:
  471.             t = s;
  472.             gotoxy(x, y);
  473.             break;
  474.  
  475.         case GOEOL:
  476.             t = s + strlen(s);
  477.             gotoxy(x + strlen(s), y);
  478.             break;
  479.  
  480.                 case RUBOUT:
  481.         case BKSPC:
  482.             if (x == wherex())
  483.                 break;
  484.             t--;
  485.             memmove(t, (t + 1), strlen(t) + 1);
  486.             gotoxy(wherex() - 1, y);
  487.             x1 = wherex();
  488.             bputs(t);
  489.             bputc(' ');
  490.             gotoxy(x1, y);
  491.             break;
  492.  
  493.         case LEFT:
  494.             if (t == s)
  495.                 break;
  496.             t--;
  497.             gotoxy(wherex() - 1, y);
  498.             break;
  499.  
  500.         case RIGHT:
  501.             if (t >= (s + strlen(s)))
  502.                 break;
  503.             t++;
  504.             gotoxy(wherex() + 1, y);
  505.             break;
  506.  
  507.         case DELCHR:
  508.             memmove(t, t + 1, strlen(t) + 1);
  509.             x1 = wherex();
  510.             bputs(t);
  511.             bputc(' ');
  512.             gotoxy(x1, y);
  513.             break;
  514.  
  515.         case INSERT:
  516.             insert = !insert;
  517.             break;
  518.  
  519.         default:
  520.             if ((strlen(s) == c) || ((ch & 0xff) == 0))
  521.                 break;
  522.             if (insert) {
  523.                 x1 = wherex();
  524.                 t++;
  525.                 strins(s, (char) (ch & 0xff), (x1 - x + 1));
  526.                 gotoxy(x, y);
  527.                 bputs(s);
  528.                 gotoxy(x1 + 1, y);
  529.             }
  530.             else {
  531.                 *t++ = (char) (ch & 0xFF);
  532.                 bputc((char) (ch & 0xff));
  533.             }
  534.             video_update();
  535.             break;
  536.         }
  537.     }
  538.     return(ABORT);        /* never reached */
  539. }
  540.  
  541. int     getnum(int l, int h, int value)
  542. {
  543.     int     i, x;
  544.         char    s[TEXTLEN];
  545.  
  546.     i = value;
  547.     x = wherex();
  548.     do {
  549.         clreol();
  550.                 memset(s, 0, sizeof(s));
  551.         itoa(i, s, 10);
  552.         gotoxy(x, wherey());
  553.         (void)bgets(s, TEXTLEN/2);
  554.         i = atoi(s);
  555.     } while ((i < l) || (i > h));
  556.     return (i);
  557. }
  558.  
  559. void    set_color(unsigned char attr)
  560.  
  561. {
  562.     current_color = attr;
  563. }
  564.  
  565. unsigned get_color()
  566.  
  567. {
  568.     return(current_color);
  569. }
  570.  
  571. int     bprintf(char *f,...)
  572. {
  573.     va_list params;
  574.     int     i;
  575.     char s[TEXTLEN];
  576.  
  577.     va_start(params, f);
  578.     i = vsprintf(s, f, params);
  579.     bputs(s);
  580.     return (i);
  581. }
  582.  
  583. void    strins(char *l, char c, int x)
  584. {
  585.     int     i = strlen(l);
  586.  
  587.     x--;
  588.  
  589.     if (i-x < 0) return;
  590.     memmove((l + x + 1), (l + x), (i - x));
  591.     *(l + x) = c;
  592. }
  593.  
  594. void    strdel(char *l, int x)
  595. {
  596.     int     i = strlen(l);
  597.  
  598.     x--;
  599.  
  600.     if (i-x+1 < 0) return;
  601.     memmove((l + x), (l + x + 1), (i - x + 1));
  602.     *(l + i) = 0;
  603. }
  604.  
  605. #if USEFOSSIL
  606.  
  607. VFOSSIL v;
  608.  
  609. static void vfossil_init ()
  610. {
  611.    char far *q;
  612.    union REGS r;
  613.    struct SREGS s;
  614.  
  615.    v.vfossil_size = sizeof (VFOSSIL);
  616.    q = (char far *) &v;
  617.  
  618.    r.h.ah = 0x81;
  619.    r.h.al = 0;
  620.  
  621.    segread (&s);
  622.    s.es = FP_SEG (q);
  623.    r.x.di = FP_OFF (q);
  624.    int86x (0x14, &r, &r, &s);
  625.  
  626.    if (r.x.ax == 0x1954)
  627.       {
  628.       /* There is a VFOSSIL out there, so set it up for use */
  629.       vfossil_open ();
  630.       }
  631.   else
  632.       {
  633.       fputs ("No Video FOSSIL installed, aborting....\n\n",stderr);
  634.       halt(255);
  635.       }
  636. }
  637.  
  638. CURSOR _cursor;
  639.  
  640. static void vfossil_cursor (int st)
  641. {
  642.    CURSOR far *q;
  643.  
  644.    if (vfossil_funcs[9])
  645.       {
  646.       q = (CURSOR far *) &_cursor;
  647.       /* We can make the cursor go away */
  648.       VioGetCurType (q, 0);
  649.       _cursor.cur_attr = st ? 0 : -1;
  650.       VioSetCurType (q, 0);
  651.       }
  652. }
  653.  
  654. static void vfossil_open ()
  655. {
  656.    char far *q;
  657.    union REGS r;
  658.    struct SREGS s;
  659.  
  660.    segread (&s);
  661.    r.h.ah = 0x81;
  662.    r.h.al = 1;
  663.    r.x.cx = 80;
  664.    q = (char far *) vfossil_funcs;
  665.    r.x.di = FP_OFF (q);
  666.    s.es = FP_SEG (q);
  667.    int86x (0x14, &r, &r, &s);
  668.    if ((r.x.ax != 0x1954) || (r.x.bx < 14))
  669.       {
  670.       fputs ("Unable to initialize Video FOSSIL, aborting....\n\n",stderr);
  671.       halt(255);
  672.       }
  673. }
  674.  
  675. static void vfossil_close ()
  676. {
  677.    union REGS r;
  678.  
  679.    vfossil_cursor (1);
  680.  
  681.    r.h.ah = 0x81;
  682.    r.h.al = 2;
  683.  
  684.    int86 (0x14, &r, &r);
  685.  
  686. }
  687. #endif
  688.